﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.Entity;
using zichan.DAL;
using System.Data.SqlClient;
using System.Data;
using System.IO;

namespace zichan.BLL
{
    public class ConsumateInfoService
    {
        public static string AddFieldsList(List<ConsumateInfo> consumateInfo)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    db.ConsumateInfos.AddRange(consumateInfo);
                    db.SaveChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return "完善信息失败";
                }
            }
        }
        public static List<ConsumateInfo> GetList(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                List<ConsumateInfo> consumateInfoList = db.ConsumateInfos.Where(o => o.AssetId == id).ToList();
                return consumateInfoList;
            }
        }
        
        public static string EditFieldList(List<ConsumateInfo> consumateInfo)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    foreach (ConsumateInfo c in consumateInfo)
                    {
                        ConsumateInfo model = db.ConsumateInfos.FirstOrDefault(o => o.Id == c.Id);
                        model.FieldContent = c.FieldContent;
                        db.SaveChanges();
                    }
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return "无法修改";
                }
            }
        }
        public static int EditFieldName(string name,int id,int assetClassId,string constr)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                int i = 0;
                //string sql = "UPDATE ConsumateInfoes SET FieldName=@name where  FieldId=@id and AssetId in(@ids)";
                List<int> assetIds = db.Assets.Where(o => o.AssetClassId == assetClassId).Select(o => o.Id).ToList();
                string ids = string.Join(",", assetIds);
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@name", name),new SqlParameter("@id",id), new SqlParameter("@ids", ids) };
                if (assetIds.Count() > 0)
                {    i=db.Database.ExecuteSqlCommand("UPDATE ConsumateInfoes SET FieldName=(N'"+name+"') WHERE FieldId="+id+" and AssetId in (" + ids + ")");
                   
                }
                return i;
            }
        }
        public static int DeleteFieldName(int fieldId)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                int i = 0;
                i = db.Database.ExecuteSqlCommand("Delete from ConsumateInfoes WHERE FieldId="+ fieldId);
                return i;
            }
        }
    }
}
